package com.qf.utils;

import com.qf.bean.Employee;
import com.qf.dao.shangpin_dao.LoginDao;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 13/12/2019
 * Echo
 * sassspringboot
 * Alibaba
 */
public class MyRealm extends AuthorizingRealm{


    @Autowired
    private LoginDao loginDao;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override//认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        String loginName = (String) authenticationToken.getPrincipal();
        Employee employee = loginDao.login(loginName);
        if (employee == null) {

            return null;
        }

        return new SimpleAuthenticationInfo(employee, employee.getePassword(), ByteSource.Util.bytes(employee.getSalt()), this.getName());


    }
}
